package com.jf.springmvc.json;

import com.jf.springmvc.json.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-11-02
 * Time:17:56
 */
@RequestMapping("/json")
//@ResponseBody
//@Controller
@RestController
public class JsonController {
    /*
    Json数据的传递
    1.发送数据的请求方法需用@ResponseBody注释,SpringMVC会识别数据类型(Bean...)并转换成Json数据格式
    2.接收的数据若想封装成Bean,参数前要用@RequestBody注释,SpringMVC会将接受数据转换格式并封装成Bean
    3.若在类上注解@ResponseBody,表示该类的所有方法都注释@ResponseBody
    4.@RestController包含了@ResponseBody和@Controller,可以用来替换
     */
    @GetMapping("/get")
    //@ResponseBody
    public User get() {
        User user= new User(1,"jack",11);
        System.out.println(user);
        return user;
    }
    @PostMapping("/add")
    //@ResponseBody
    public User add(@RequestBody User user) {
        user.setId(99);
        System.out.println(user);
        return user;
    }
    @GetMapping("/getList")
    //@ResponseBody
    public List<User> getList() {
        List<User> list= new ArrayList<>();
        list.add(new User(1,"jack",11));
        list.add(new User(2,"tom",22));
        list.add(new User(3,"mary",33));
        return list;
    }
}
